﻿#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2012-07-14
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012 ajayumi 版权所有
'————————————————————————————————————————
#End Region
Imports System.Runtime.Serialization
Imports System.Xml

Namespace Xml.Serialization

    Public Class GenericFormatter(Of U As {IFormatter, New})
        Implements IGenericFormatter

        Private m_IFormatter As IFormatter = New U()

        Public Function DeSerialize(Of T)(ByVal serializationStream As System.IO.Stream) As T Implements IGenericFormatter.DeSerialize
            Try
                Return DirectCast(Me.m_IFormatter.Deserialize(serializationStream), T)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function DeSerialize(ByVal serializationStream As System.IO.Stream) As Object() Implements IGenericFormatter.DeSerialize
            Try
                Return Me.DeSerialize(serializationStream, -1)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function DeSerialize(ByVal serializationStream As System.IO.Stream, ByVal count As Integer) As Object() Implements IGenericFormatter.DeSerialize
            Dim _ArrayList As New ArrayList()
            Try
                Dim i As Integer = 0
                While count < 0 OrElse i < count
                    _ArrayList.Add(DeSerialize(Of Object)(serializationStream))
                    i += 1
                End While
            Catch ex As XmlException
                Throw ex
            Catch ex As Exception
                Throw ex
            End Try
            Return _ArrayList.ToArray()
        End Function

        Public Sub Serialize(Of T)(ByVal serializationStream As System.IO.Stream, ByVal model As T) Implements IGenericFormatter.Serialize
            Me.m_IFormatter.Serialize(serializationStream, model)
        End Sub

        Public Sub Serialize(Of T)(ByVal serializationStream As System.IO.Stream, ByVal model As T, ByVal ParamArray params() As Object) Implements IGenericFormatter.Serialize
            Me.Serialize(serializationStream, model)
            For Each item In params
                Me.Serialize(serializationStream, item)
            Next
        End Sub

    End Class

End Namespace


'---------------------------------------------
'单个对象序列化，反序列化实例。
'[Serializable]
'Class Program
'    {
'        static void Main(string[] args)
'        {
'            Program pro = new Program();
'            pro.Name = "ceshi1";
'            GenericFormatter<SoapFormatter> soap = new GenericFormatter<SoapFormatter>();
'            FileStream stream = new FileStream(@"C:/FlashPast.xml", FileMode.Create, FileAccess.ReadWrite);
'            soap.Serialize(stream, pro);
'            // 把流中位置归零。
'            stream.Seek(0, System.IO.SeekOrigin.Begin);
'            Program newPro = soap.Deserialize<Program>(stream);
'            Console.ReadLine();
'        }
'        public string Name { get; set; }
'    }
'    // FlashPast.xml
'<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/     XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
'<SOAP-ENV:Body>
'<a1:Program id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyConsoleApplication/MyConsoleApplication%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
'<_x003C_Name_x003E_k__BackingField id="ref-3">
'ceshi1
'</_x003C_Name_x003E_k__BackingField>
'</a1:Program>
'</SOAP-ENV:Body>
'</SOAP-ENV:Envelope>

'---------------------------------------------
'多个对象序列化，反序列化实例
'[Serializable]
'Class Program
'    {
'        static void Main(string[] args)
'        {
'            Program pro = new Program();
'            pro.Name = "ceshi1";
'            Program pro1 = new Program();
'            pro1.Name = "ceshi2";
'            GenericFormatter<SoapFormatter> soap = new GenericFormatter<SoapFormatter>();
'            FileStream stream = new FileStream(@"C:/FlashPast.xml", FileMode.Create, FileAccess.ReadWrite);
'            soap.Serialize(stream, pro, pro1);
'            // 把流中位置归零。
'            stream.Seek(0, System.IO.SeekOrigin.Begin);
'            object[] arry = soap.Deserialize(stream);
'            Console.ReadLine();
'        }
'        public string Name { get; set; }
'    }